﻿using DeviceCommons.DataHandling;
using DeviceCommons.DeviceMessages.Abstractions;
using DeviceCommons.DeviceMessages.Models.V1;
using System.Buffers;

namespace DeviceCommons.DeviceMessages.Serialization.V2.Serializers
{
    public class DeviceMessageInfoReadingStatesSerializer : AbstractMessageSerializer<IDeviceMessageInfoReadingStates>, IDeviceMessageInfoReadingStatesSerializer
    {
        public DeviceMessageInfoReadingStatesSerializer() : base(new DeviceMessageInfoReadingStates())
        {
        }

        public override void Serializer(ArrayBufferWriter<byte> writer, IDeviceMessageInfoReadingStates message)
        {
            ArgumentNullException.ThrowIfNull(message);
            ArgumentNullException.ThrowIfNull(message.StateArray);
            writer.Write([message.Count]);
            if (message.Count > 0)
            {
                foreach (var state in message.StateArray)
                {
                    DeviceMessageSerializerProvider.InfoReadingStateV2Ser.Serializer(writer, state);
                }
            }
        }
    }
}
